#include "myplaceway.h"
#include "hqlabel.h"

MyPlaceWay::MyPlaceWay()
{
    setSpacing(0);
    setContentsMargins(0, 0, 0, 0);
}
MyPlaceWay::~MyPlaceWay() {}



MyPlaceWayPh::MyPlaceWayPh() {}
MyPlaceWayPh::~MyPlaceWayPh() {}

MyPlaceWayPv1::MyPlaceWayPv1()
{
    m_scene1 = NULL;
    m_scene2 = NULL;
}
MyPlaceWayPv1::~MyPlaceWayPv1()
{
    //此处scene放置在view上之后view作为子控件析构的时候会自动删除，
    //但是实践证明scene不会被自动删除，所以在析构中手动将其删除，否则将内存泄漏!!!
    if (m_scene1 != NULL)
    {
        m_scene1->clear();
        delete m_scene1;
        m_scene1 = NULL;
    }
    if (m_scene2 != NULL)
    {
        m_scene2->clear();
        delete m_scene2;
        m_scene2 = NULL;
    }
}
MyPlaceWayPv2::MyPlaceWayPv2()
{
    m_scene1 = NULL;
    m_scene2 = NULL;
}
MyPlaceWayPv2::~MyPlaceWayPv2()
{
    //此处scene放置在view上之后view作为子控件析构的时候会自动删除，
    //但是实践证明scene不会被自动删除，所以在析构中手动将其删除，否则将内存泄漏!!!
    if (m_scene1 != NULL)
    {
        m_scene1->clear();
        delete m_scene1;
        m_scene1 = NULL;
    }
    if (m_scene2 != NULL)
    {
        m_scene2->clear();
        delete m_scene2;
        m_scene2 = NULL;
    }
}


MyPlaceWayPANY::MyPlaceWayPANY(int digree) : m_digree(digree)
{
    m_scene = NULL;
}
MyPlaceWayPANY::~MyPlaceWayPANY()
{
    if (m_scene != NULL)
    {
        m_scene->clear();
        delete m_scene;
        m_scene = NULL;
    }
}


void MyPlaceWayPh::SetParam(QString text, QString qss1, QString qss2)
{
    if (!text.contains("@@"))
    {
        m_parent->setText(text);
        m_parent->setStyleSheet(qss1);
    } else
    {
        QStringList list = text.split("@@");
        m_label1->setText(list.at(0));
        m_label2->setText(list.at(1));
        m_label1->setStyleSheet(qss1);
        m_label2->setStyleSheet(qss2);
    }
}
void MyPlaceWayPh::SetWidget(HqLabel *parent, QString text, QString qss1, QString qss2)
{
    m_parent = parent;
    if (!text.contains("@@"))
    {
        parent->setText(text);
        parent->setStyleSheet(qss1);

        //根据情况启动滚动文本
        parent->StartScrollText(qss1);
    } else
    {
        QStringList list = text.split("@@");

        m_label1 = new QLabel(list.at(0));
        m_label2 = new QLabel(list.at(1));
        m_label1->setAlignment(Qt::AlignBottom | Qt::AlignHCenter);
        m_label2->setAlignment(Qt::AlignTop | Qt::AlignHCenter);
        m_label1->setFixedSize(parent->width(), parent->height()/2);
        m_label2->setFixedSize(parent->width(), parent->height()/2);
        m_label1->setStyleSheet(qss1);
        m_label2->setStyleSheet(qss2);

        this->setDirection(TopToBottom);
        this->addWidget(m_label1);
        this->addWidget(m_label2);

        parent->setLayout(this);
    }
}

void MyPlaceWayPh::ResetHqtext(QString text)
{
    if (!text.contains("@@"))
    {
        m_parent->setText(text);
    } else
    {
        QStringList list = text.split("@@");
        m_label1->setText(list.at(0));
        m_label2->setText(list.at(1));
    }
}

void MyPlaceWayPv1::SetParam(QString text, QString qss1, QString qss2)
{
    if (!text.contains("@@"))
    {
        myapp::insertStrEveryChar(text, "<br>");
        QString html_text = QString("<html><head/><body><p style='line-height:2px'>%1</p></body></html>").arg(text);
        m_parent->setText(html_text);
        m_parent->setStyleSheet(qss1);
    }
    else
    {
        QStringList list = text.split("@@");
        QString ch = list.at(0);
        //每隔一个字插入一个 <br> 作为回车
        myapp::insertStrEveryChar(ch, "<br>");

        QString html = QString("<html><head/><body><p style='line-height:80%'>%1</p></body></html>").arg(ch);
        m_label1->setText(html);
        m_label2->setText(list.at(1));
        m_label1->setStyleSheet(qss1);
        m_label2->setStyleSheet(qss2);
    }
}
void MyPlaceWayPv1::SetWidget(HqLabel *parent, QString text, QString qss1, QString qss2)
{
    m_parent = parent;
    if (!text.contains("@@"))
    {
        myapp::insertStrEveryChar(text, "<br>");
        QString html_text = QString("<html><head/><body><p style='line-height:2px'>%1</p></body></html>").arg(text);
        parent->setText(html_text);
        parent->setStyleSheet(qss1);
    }
    else
    {
        QStringList list = text.split("@@");
        QString ch = list.at(0);
        //每隔一个字插入一个 <br> 作为回车
        myapp::insertStrEveryChar(ch, "<br>");

        QString html = QString("<html><head/><body><p style='line-height:80%'>%1</p></body></html>").arg(ch);
        m_label1 = new QLabel(html);
        m_label2 = new QLabel(list.at(1));
        m_label1->setStyleSheet(qss1);
        m_label2->setStyleSheet(qss2);
        m_label1->resize(parent->width()/2, parent->height());
        m_label2->resize(parent->height(), parent->width()/2);

        m_label1->setAlignment(Qt::AlignRight | Qt::AlignTop);
        m_label2->setAlignment(Qt::AlignLeft | Qt::AlignBottom);

        m_scene1 = new QGraphicsScene();
        m_scene2 = new QGraphicsScene();

        m_view1 = new QGraphicsView();
        m_view2 = new QGraphicsView();
        m_view1->setScene(m_scene1);
        m_view2->setScene(m_scene2);

        //此处addwidget消耗时间
        m_scene1->addWidget(m_label1);
        m_view1->centerOn(0, 0);
        m_view1->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
        m_view1->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);

        //此处addwidget消耗时间
        m_scene2->addWidget(m_label2);
        m_view2->rotate(90);
        m_view2->centerOn(0, 0);
        m_view2->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
        m_view2->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);

        m_view1->setContentsMargins(0, 0, 0, 0);
        m_view1->setStyleSheet("background:transparent; border:none");
        m_view2->setContentsMargins(0, 0, 0, 0);
        m_view2->setStyleSheet("background:transparent; border:none");

        this->setDirection(LeftToRight);
        this->addWidget(m_view1);
        this->addWidget(m_view2);
        parent->setLayout(this);
    }
}
void MyPlaceWayPv1::ResetHqtext(QString text)
{
    if (!text.contains("@@"))
    {
        myapp::insertStrEveryChar(text, "<br>");
        QString html_text = QString("<html><head/><body><p style='line-height:2px'>%1</p></body></html>").arg(text);
        m_parent->setText(html_text);
    }
    else
    {
        QStringList list = text.split("@@");
        QString ch = list.at(0);
        //每隔一个字插入一个 <br> 作为回车
        myapp::insertStrEveryChar(ch, "<br>");

        QString html = QString("<html><head/><body><p style='line-height:80%'>%1</p></body></html>").arg(ch);
        m_label1->setText(html);
        m_label2->setText(list.at(1));
    }
}

void MyPlaceWayPv2::SetParam(QString text, QString qss1, QString qss2)
{
    if (!text.contains("@@"))
    {
        myapp::insertStrEveryChar(text, "<br>");
        QString html_text = QString("<html><head/><body><p style='line-height:2px'>%1</p></body></html>").arg(text);
        m_parent->setText(html_text);
        m_parent->setStyleSheet(qss1);
    }
    else
    {
        QStringList list = text.split("@@");
        QString ch = list.at(0);
        //每隔一个字插入一个 <br> 作为回车
        myapp::insertStrEveryChar(ch, "<br>");

        QString html = QString("<html><head/><body><p style='line-height:80%'>%1</p></body></html>").arg(ch);
        m_label1->setText(list.at(1));
        m_label2->setText(html);
        m_label1->setStyleSheet(qss1);
        m_label2->setStyleSheet(qss2);
    }
}
void MyPlaceWayPv2::SetWidget(HqLabel *parent, QString text, QString qss1, QString qss2)
{
    m_parent = parent;
    if (!text.contains("@@"))
    {
        myapp::insertStrEveryChar(text, "<br>");
        QString html_text = QString("<html><head/><body><p style='line-height:2px'>%1</p></body></html>").arg(text);
        parent->setText(html_text);
        parent->setStyleSheet(qss1);
    }
    else
    {
        QStringList list = text.split("@@");
        QString ch = list.at(0);
        //每隔一个字插入一个 <br> 作为回车
        myapp::insertStrEveryChar(ch, "<br>");

        QString html = QString("<html><head/><body><p style='line-height:80%'>%1</p></body></html>").arg(ch);
        m_label1 = new QLabel(list.at(1));
        m_label2 = new QLabel(html);
        m_label1->setStyleSheet(qss1);
        m_label2->setStyleSheet(qss2);
        m_label1->resize(parent->width(), parent->height()/2);
        m_label2->resize(parent->height()/2, parent->width());

        m_label1->setAlignment(Qt::AlignLeft | Qt::AlignTop);
        m_label2->setAlignment(Qt::AlignLeft | Qt::AlignTop);

        m_scene1 = new QGraphicsScene();
        m_scene2 = new QGraphicsScene();

        m_view1 = new QGraphicsView();
        m_view2 = new QGraphicsView();
        m_view1->setScene(m_scene1);
        m_view2->setScene(m_scene2);

        m_scene1->addWidget(m_label1);
        m_view1->rotate(90);
        m_view1->centerOn(0, 0);
        m_view1->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
        m_view1->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);

        m_scene2->addWidget(m_label2);
        m_view2->centerOn(0, 0);
        m_view2->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
        m_view2->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);

        m_view1->setContentsMargins(0, 0, 0, 0);
        m_view1->setStyleSheet("background:transparent; border:none");
        m_view2->setContentsMargins(0, 0, 0, 0);
        m_view2->setStyleSheet("background:transparent; border:none");

        this->setDirection(LeftToRight);
        this->addWidget(m_view1);
        this->addWidget(m_view2);
        parent->setLayout(this);
    }
}
void MyPlaceWayPv2::ResetHqtext(QString text)
{
    if (!text.contains("@@"))
    {
        myapp::insertStrEveryChar(text, "<br>");
        QString html_text = QString("<html><head/><body><p style='line-height:2px'>%1</p></body></html>").arg(text);
        m_parent->setText(html_text);
    }
    else
    {
        QStringList list = text.split("@@");
        QString ch = list.at(0);
        //每隔一个字插入一个 <br> 作为回车
        myapp::insertStrEveryChar(ch, "<br>");

        QString html = QString("<html><head/><body><p style='line-height:80%'>%1</p></body></html>").arg(ch);
        m_label1->setText(list.at(1));
        m_label2->setText(html);
    }
}


void MyPlaceWayPANY::SetParam(QString text, QString qss1, QString qss2)
{
    if (!text.contains("@@"))
    {
        m_label1->setText(text);
        m_label1->setStyleSheet(qss1);
    } else
    {
        QStringList list = text.split("@@");

        m_label1->setText(list.at(0));
        m_label2->setText(list.at(1));
        m_label1->setStyleSheet(qss1);
        m_label2->setStyleSheet(qss2);
    }
}
void MyPlaceWayPANY::SetWidget(HqLabel *parent, QString text, QString qss1, QString qss2)
{
    m_parent = parent;
    if (!text.contains("@@"))
    {
        m_label1 = new QLabel(text);
        m_label1->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
        m_label1->setStyleSheet(qss1);
        m_label1->setFixedSize(parent->width(), parent->height());
        m_scene = new QGraphicsScene();
        m_view = new QGraphicsView(m_scene);
        m_view->setFixedSize(parent->width(), parent->height());
        //设置场景区域和视图大小一样大，且场景坐标系在视图坐标系中间
        m_scene->setSceneRect(-parent->width()/2, -parent->height()/2, parent->width(), parent->height());

        m_pw = m_scene->addWidget(m_label1);
        //设置图元是在左顶点开始显示
        m_pw->setPos(m_scene->sceneRect().left(), m_scene->sceneRect().top());
        m_view->rotate(m_digree);
        m_view->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
        m_view->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
        m_view->setStyleSheet("background:transparent; border:none");
        m_view->setContentsMargins(0, 0, 0, 0);

        this->addWidget(m_view);
        parent->setLayout(this);
    } else
    {
        QStringList list = text.split("@@");

        m_label1 = new QLabel(list.at(0));
        m_label2 = new QLabel(list.at(1));
        m_label1->setAlignment(Qt::AlignBottom | Qt::AlignHCenter);
        m_label2->setAlignment(Qt::AlignTop | Qt::AlignHCenter);
        m_label1->setFixedSize(parent->width(), parent->height()/2);
        m_label2->setFixedSize(parent->width(), parent->height()/2);
        m_label1->setStyleSheet(qss1);
        m_label2->setStyleSheet(qss2);

        QVBoxLayout *pLayout = new QVBoxLayout();
        pLayout->addWidget(m_label1);
        pLayout->addWidget(m_label2);
        pLayout->setSpacing(0);
        pLayout->setContentsMargins(0, 0, 0, 0);

        QWidget *w = new QWidget();
        w->setLayout(pLayout);
        w->setStyleSheet("background:transparent; border:none;");

        m_scene = new QGraphicsScene();
        m_scene->setSceneRect(-parent->width()/2, -parent->height()/2, parent->width(), parent->height());
        m_view = new QGraphicsView(m_scene);
        m_view->setFixedSize(parent->width(), parent->height());
        m_pw = m_scene->addWidget(w);
        m_pw->setPos(m_scene->sceneRect().left(), m_scene->sceneRect().top());
        m_view->rotate(m_digree);
        m_view->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
        m_view->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
        m_view->setStyleSheet("background:transparent; border:none");
        m_view->setContentsMargins(0, 0, 0, 0);

        this->addWidget(m_view);
        parent->setLayout(this);
    }
}

void MyPlaceWayPANY::ResetHqtext(QString text)
{
    if (!text.contains("@@"))
    {
        m_label1->setText(text);

    } else
    {
        QStringList list = text.split("@@");
        m_label1->setText(list.at(0));
        m_label2->setText(list.at(1));
    }
}
